package com.ylsk.mobilecheck.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.google.api.client.util.Lists;
import com.ylsk.check.domain.*;
import com.ylsk.check.domain.dto.*;
import com.ylsk.check.domain.emum.BasicInfoTemplateEnum;
import com.ylsk.check.domain.emum.ConstructionStatusEnum;
import com.ylsk.check.domain.emum.ReportStatusEnum;
import com.ylsk.check.domain.emum.SearchTypeEnum;
import com.ylsk.check.domain.so.*;
import com.ylsk.check.domain.vo.*;
import com.ylsk.check.service.*;
import com.ylsk.common.annotation.DataScope;
import com.ylsk.common.utils.SecurityUtils;
import com.ylsk.common.utils.StringUtils;
import com.ylsk.mobilecheck.mapper.ConstructionMobileMapper;
import com.ylsk.mobilecheck.service.ConstructionMobileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 施工端Service业务层处理
 *
 * @author ysy
 * @date 2023-12-03
 */
@Service
public class ConstructionMobileServiceImpl implements ConstructionMobileService
{
    @Autowired
    private IReportInfoService reportInfoService;

    @Autowired
    private IExpresswaySectionService expresswaySectionService;

    @Autowired
    private ConstructionMobileMapper constructionMobileMapper;

    @Autowired
    private IReportApprLogService reportApprLogService;

    @Autowired
    private IEntryReportService entryReportService;

    @Autowired
    private IWithdrawalReportService withdrawalReportService;

    @Autowired
    private IJeevesReportService jeevesReportService;

    @Autowired
    private IChangeReportService changeReportService;
    @Autowired
    private ICheckProblemService checkProblemService;
    @Override
    public int saveReportInfo(ReportInfoDTO reportInfoDTO) {
        return reportInfoService.insertreportInfo(reportInfoDTO);
    }

    @Override
    public ReportInfoVO getReportInfo(Long reportInfoId) {
        return reportInfoService.selectreportInfoByreportInfoId(reportInfoId);
    }

    @Override
    public int updateReportInfo(ReportInfoDTO reportInfoDTO) {
        return reportInfoService.updatereportInfo(reportInfoDTO);
    }

    @Override
    public int removeReportInfo(Long reportInfoId) {
        return reportInfoService.deletereportInfoByreportInfoId(reportInfoId);
    }

    @Override
    @DataScope(deptAlias = "sd",userAlias = "su")
    public List<ReportInfoVO> listReportInfo(ReportInfoSO reportInfoSO) {
        String searchType = reportInfoSO.getSearchType();
        if(StringUtils.isEmpty(searchType)){
            return Lists.newArrayList();
        }

        if(searchType.equals(SearchTypeEnum.AUDIT.getCode())){
            reportInfoSO.setOperateUserId(SecurityUtils.getUserId());
            return constructionMobileMapper.selectAuditReportInfoList(reportInfoSO);
        }else{
            if(searchType.equals(SearchTypeEnum.REPORT_RECORD.getCode())){
                reportInfoSO.setReportStatus(ReportStatusEnum.PASS.getCode());
            }
            List<ReportInfoVO> reportInfoVOList = constructionMobileMapper.selectReportInfoList(reportInfoSO);
            for (ReportInfoVO reportInfoVO : reportInfoVOList) {
                String basicInfo = reportInfoVO.getBasicInfo();
                String tbName = reportInfoVO.getTbName();
                Map<String, Object> objectMap = BasicInfoTemplateEnum.getBasicInfo(tbName, basicInfo.split(","));
                reportInfoVO.setBasicInfoMap(objectMap);
            }
            return reportInfoVOList;
        }

    }

    @Override
    @DataScope(deptAlias = "te",userAlias = "tf")
    public List<ReportInfoVO> selectListReportInfo(ReportInfoSO reportInfoSO) {
        return reportInfoService.selectreportInfoList(reportInfoSO);
    }

    @Override
    public List<ReportApprLogVO> listReportInfoApprRecord(Long reportInfoId) {
        ReportApprLog reportApprLog = new ReportApprLog();
        reportApprLog.setReportInfoId(reportInfoId);
        return reportApprLogService.selectReportApprLogList(reportApprLog);
    }

    @Override
    public List<ExpresswaySectionVO> getExpresswaySectionList(ExpresswaySection expresswaySection) {
        return expresswaySectionService.selectExpresswaySectionList(expresswaySection);
    }

    @Override
    public List<EntryReportVO> listEntryReport(EntryReportSO entryReportSO) {
        return entryReportService.selectEntryReportList(entryReportSO);
    }

    @Override
    public int saveEntryReport(EntryReportDTO entryReportDTO) {
        return entryReportService.insertEntryReport(entryReportDTO);
    }

    @Override
    public EntryReportVO getEntryReport(Long entryReportId) {
        return entryReportService.selectEntryReportByEntryReportId(entryReportId);
    }

    @Override
    public List<WithdrawalReportVO> listWithdrawalReport(WithdrawalReportSO withdrawalReportSO) {
        return withdrawalReportService.selectWithdrawalReportList(withdrawalReportSO);
    }

    @Override
    public int saveWithdrawalReport(WithdrawalReportDTO withdrawalReportDTO) {
        return withdrawalReportService.insertWithdrawalReport(withdrawalReportDTO);
    }

    @Override
    public WithdrawalReportVO getWithdrawalReport(Long withdrawalReportId) {
        return withdrawalReportService.selectWithdrawalReportByWithdrawalReportId(withdrawalReportId);
    }

    @Override
    public List<JeevesReportVO> listJeevesReport(JeevesReportSO jeevesReportSO) {
        return jeevesReportService.selectJeevesReportList(jeevesReportSO);
    }

    @Override
    public int saveJeevesReport(JeevesReportDTO jeevesReportDTO) {
        return jeevesReportService.insertJeevesReport(jeevesReportDTO);
    }

    @Override
    public JeevesReportVO getJeevesReport(Long jeevesReportId) {
        return jeevesReportService.selectJeevesReportByJeevesReportId(jeevesReportId);
    }

    @Override
    public List<ChangeReportVO> listChangeReport(ChangeReportSO changeReportSO) {
        return changeReportService.selectChangeReportList(changeReportSO);
    }

    @Override
    public int saveChangeReport(ChangeReportDTO changeReportDTO) {
        return changeReportService.insertChangeReport(changeReportDTO);
    }

    @Override
    public ChangeReportVO getChangeReport(Long changeReportId) {
        return changeReportService.selectChangeReportByChangeReportId(changeReportId);
    }

    @Override
    public List<CheckProblemVO> listCheckProblem(CheckProblemSO checkProblemSO) {
        return constructionMobileMapper.selectCheckProblemList(checkProblemSO);
    }

    @Override
    public int updateCheckProblem(RectificationReportDTO rectificationReportDTO) {
        return checkProblemService.saveRectificationReport(rectificationReportDTO);
    }

    @Override
    public CheckProblemVO getCheckProblem(Long checkProblemId) {
        return checkProblemService.selectCheckProblemByCheckProblemId(checkProblemId);
    }

    @Override
    public List<ReportInfoVO> listProjectEnd(ReportInfoSO reportInfoSO) {
        reportInfoSO.setConstructionStatus(ConstructionStatusEnum.END.getCode());
        return reportInfoService.selectreportInfoList(reportInfoSO);
    }

    @Override
    public List<JeevesReportVO> selectJeevesNotWithdrawnList(JeevesReportSO jeevesReportSO) {
        return jeevesReportService.selectJeevesNotWithdrawnList(jeevesReportSO);
    }

    public static void main(String[] args) {
        String msg = "jeevesNum=ZDBB-20240117001,jeevesStartDate=2024-01-17,jeevesEndDate=2024-01-17";
        Map map = Arrays.stream(msg.split(","))
                .map(elem -> elem.split("="))
                .collect(Collectors.toMap(e -> e[0], e -> e[1]));
        System.out.println(JSONObject.toJSONString(map));
    }
}
